import 'dart:async';

import 'package:flutter/material.dart';
import 'package:time/data/start_up_bean.dart';
import 'package:time/http/callback/http_request_callback.dart';
import 'package:time/http/http_request.dart';

class StartUpApp extends StatelessWidget {
  const StartUpApp({Key? key}) : super(key: key);

  @override
  Widget build(BuildContext context) {
    return MaterialApp(
        title: "启动成功率测试",
        theme: ThemeData(primarySwatch: Colors.blue),
        home: const StartUpPage());
  }
}

class StartUpPage extends StatefulWidget {
  const StartUpPage({Key? key}) : super(key: key);

  @override
  State<StatefulWidget> createState() {
    return StartUpState();
  }
}

class StartUpState extends State<StatefulWidget> {
  List<StartUpBean>? startUpList;
  late Timer timer;

  StartUpState() {
    startRequestClock();
  }

  void startRequestClock() {
    requestUrl();
    timer = Timer.periodic(const Duration(seconds: 1), (timer) {
      requestUrl();
    });
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
        appBar: AppBar(
          title: const Text("启动成功率测试"),
          actions: [
            FloatingActionButton(
              onPressed: () {
                resetUrl();
              },
              child: const Text("重置"),
            )
          ],
        ),
        body: ListView.builder(
          itemBuilder: createItem,
          itemCount: startUpList == null ? 1 : (startUpList!.length),
        ));
  }

  Widget createItem(BuildContext context, int index) {
    StartUpBean itemBean = index == 0 ? StartUpBean() : startUpList![index - 1];
    TextStyle textStyle = const TextStyle(fontSize: 14);

    double width = MediaQuery.of(context).size.width / 6, height = 40;
    return Column(
      children: [
        Row(
          children: [
            SizedBox(
              width: width,
              height: height,
              child: Center(
                child: Text(
                  index == 0
                      ? "名称"
                      : (itemBean.title == null ? "" : itemBean.title!),
                  style: textStyle,
                  textAlign: TextAlign.center,
                ),
              ),
            ),
            SizedBox(
              width: width,
              height: height,
              child: Center(
                child: Text(
                  index == 0
                      ? "参数"
                      : (itemBean.param == null ? 0 : itemBean.param!)
                          .toString(),
                  style: textStyle,
                ),
              ),
            ),
            SizedBox(
              width: width,
              height: height,
              child: Center(
                child: Text(
                  index == 0
                      ? "总次数"
                      : (itemBean.totalNum == null ? 0 : itemBean.totalNum!)
                          .toString(),
                  style: textStyle,
                ),
              ),
            ),
            SizedBox(
                width: width,
                height: height,
                child: Center(
                  child: Text(
                    index == 0
                        ? "错误"
                        : (itemBean.errNum == null ? 0 : itemBean.errNum!)
                            .toString(),
                    style: textStyle,
                  ),
                )),
            SizedBox(
              width: width,
              height: height,
              child: Center(
                child: Text(
                  index == 0
                      ? "首次"
                      : (itemBean.firstErrNum == null
                              ? 0
                              : itemBean.firstErrNum!)
                          .toString(),
                  style: textStyle,
                ),
              ),
            ),
            SizedBox(
              width: width,
              height: height,
              child: Center(
                child: Text(
                  index == 0
                      ? "成功率"
                      : (itemBean.errRatio == null ? "" : itemBean.errRatio!),
                  style: textStyle,
                ),
              ),
            )
          ],
        ),
        const Divider(
          height: 1,
        )
      ],
    );
  }

  void requestUrl() async {
    HttpRequest<StartUpBean>().requestGet(
        "log/getStartLogInfo",
        HttpRequestCallback<StartUpBean>("StartUpBean", onSuccessList: (value) {
          value?.add(StartUpBean());
          setState(() {
            startUpList = value;
          });
        }, onError: (msg, code) {}),
        {});
  }

  void resetUrl() async {
    HttpRequest<bool>().requestGet(
        "log/resetStartLog",
        HttpRequestCallback<bool>("bool",
            onSuccess: (value) {}, onError: (msg, code) {}),
        {});
  }

  @override
  void dispose() {
    super.dispose();
    timer.cancel();
  }
}
